﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetCore.Mvc.Extensions;
using FwProject.Controllers;
using FwProject.FactoryInfo;
using FwProject.FactoryInfo.Dto;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Controllers
{
    [AbpMvcAuthorize]
    public class FactoryController: FwProjectControllerBase
    {
        private readonly IFactoryNameInfoAppService _factoryNameInfoAppService;

        public FactoryController(IFactoryNameInfoAppService factoryNameInfoAppService)
        {
            _factoryNameInfoAppService = factoryNameInfoAppService;
        }
        /// <summary>
        /// 获取工厂列表
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<ActionResult> Index(int pageIdx,int size,DateTime? beginTime,DateTime? endTime,string keyword="")
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;

            var factory = await _factoryNameInfoAppService.GetPagedListAsync(pageIdx,size,beginTime,endTime,keyword);
            if (Request.IsAjaxRequest())
            {
                return PartialView("IndexPartial", factory);
            }
            return View(factory);
        }
        /// <summary>
        /// 创建工厂
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> CreateFactory(FactoryNameInfoInput input)
        {
            var model = await _factoryNameInfoAppService.Create(input);
            return Json(model);
        }
        /// <summary>
        /// 修改工厂信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> Edit(Guid id)
        {
            var model = await _factoryNameInfoAppService.GetByIdAsync(id);
            return View(model);
        }
        [HttpPost]
        public async Task<JsonResult> EditFactory(Guid id, FactoryNameInfoInput input)
        {
            await _factoryNameInfoAppService.Update(id,input);
            return Json(id);
        }

        /// <summary>
        /// 删除工厂
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> DeleteFactory(Guid id)
        {
            await _factoryNameInfoAppService.Delete(id);
            return Json(id);
        }

        public async Task<JsonResult> GetAll(string keyword="")
        {
            var list = await _factoryNameInfoAppService.GetAllAsync(keyword);
            return Json(list);
        }

        public async Task<JsonResult> GetFactoryTree(bool isType=true)
        {
            var list = await _factoryNameInfoAppService.GetFactoryTreeAsync(isType);
            return Json(list);
        }
    }
}
